package com.lagou.cl.controller;

import com.lagou.cl.pojo.COrder;
import com.lagou.cl.service.COrderService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.Random;
@RestController
@RequestMapping("/corder")
public class COrderController {

	private final COrderService cOrderService;

	public COrderController(COrderService cOrderService) {
		this.cOrderService = cOrderService;
	}


	/**
	 * 给指定用户随机投递某个公司
	 *
	 * @param userId 用户Id
	 */
	@RequestMapping("/addCOrder/{userId}")
	public void addCOrder(@PathVariable("userId") Integer userId) {

		COrder cOrder = new COrder();

		cOrder.setUserId(userId);

		//随机投递到某个公司
		int companyId = new Random().nextInt(10 * userId);
		cOrder.setCompanyId(companyId);

		cOrder.setPositionId(100);
		cOrder.setDel(false);
		cOrder.setStatus("WAIT");
		cOrder.setResumeType(0);
		cOrder.setPublishUserId(111);

		long now = System.currentTimeMillis();
		cOrder.setCreateTime(new Timestamp(now));
		cOrder.setUpdateTime(new Timestamp(now));

		cOrderService.addCOrder(cOrder);

	}

	@RequestMapping("queryByCondition")
	public List<COrder> queryByCondition(@RequestParam Integer userId) {
		List<COrder> cOrderList = null;
		if (userId > 0) {
			cOrderList = cOrderService.findByUserId(userId);
		} else {
			cOrderList = cOrderService.findAll();
		}

		return cOrderList;
	}

}
